<?php

namespace App\Admin\Controllers;

use App\Models\PetSanArea;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class PetSanAreaController extends AdminController
{

    /**
     * @return mixed
     * 获取选择项
     */
    public function getName(Request $request){
        $cityId = $request->get('q');
        return PetSanArea::where('city_code', $cityId)->select(['area_code as id','name as text'])->get();
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PetSanArea(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('area_code');
            $grid->column('name');
            $grid->column('city_code');
            $grid->column('province_code');
            $grid->column('lat');
            $grid->column('lng');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PetSanArea(), function (Show $show) {
            $show->field('id');
            $show->field('area_code');
            $show->field('name');
            $show->field('city_code');
            $show->field('province_code');
            $show->field('lat');
            $show->field('lng');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PetSanArea(), function (Form $form) {
            $form->display('id');
            $form->text('area_code');
            $form->text('name');
            $form->text('city_code');
            $form->text('province_code');
            $form->text('lat');
            $form->text('lng');
        });
    }
}
